var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 当前设备是否为苹果
    isIphoneX:0,
    // 传入的订单id
    id: 0,
    // 加载动画
    loading: 0,
    // 可团购商品列表
    productList: [],
    // 选择商品的总数
    goodsNumber: 0,
    // 已选商品ID
    checkedProductIDS: [],
    // 打开商品列表默认全选里面的商品
    checkedAllStatus: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderId = options.id
    if (orderId > 0) {
      // 获取指定团购订单详细信息
      this.getTeamOrderInfo(orderId);
      this.setData({
        id: orderId
      })
    } else {
      // 获取可团购商品列表
      this.getTeamProductList()
    }
    // 获取设备信息
    let phone = wx.getStorageSync('isIphoneX')
    if (phone) {
      this.setData({
        isIphoneX: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTeamProductList()
  },
  // 获取可以团购的商品列表
  getTeamProductList() {
    let that = this;
    util.requestApi(api.getTeamProductList).then(function (res) {
      if (res.code == 200) {
        that.setData({
          productList: res.content,
          loading:1
        })
        // 判断是否为编辑商品界面
        if (that.data.id !== 0) {
          that.goodsSelectProduct();
        }
      }
    })
  },
  // 把得到的已选商品信息标为已选
  goodsSelectProduct() {
    let productIds = this.data.checkedProductIDS;
    let productList = this.data.productList;
    for (let i = 0; i < productIds.length; i++) {
      for (let j = 0; j < productList.length; j++) {
        if (productIds[i] === productList[j].productID) {
          productList[j].checked = 1
        }
      }

    }
    this.setData({
      productList: productList
    })
    this.isCheckedAll()
  },
  //判断商品是否已全选
  isCheckedAll: function () {
    let that = this
    let Ids = that.data.checkedProductIDS;
    let productList = that.data.productList;
    let productNum = 0
    for (let i = 0; i < Ids.length; i++) {
      for (let j = 0; j < productList.length; j++) {
        if (Ids[i] == productList[j].productID) {
          productNum++;
        }
      }
    }
    if (productNum == productList.length) {
      that.setData({
        checkedAllStatus: true,
      })
    }
  },
  // 点击选中与取消商品
  checkedItem: function (e) {
    let itemIndex = e.currentTarget.dataset.index;
    let that = this;
    let productList = that.data.productList;
    productList[itemIndex].checked = !productList[itemIndex].checked
    let trueNumber = 0;
    for (let i = 0; i < productList.length; i++) {
      productList[i].checked == 1 ? trueNumber++ : ''
    }
    let allSelect = false;
    if (trueNumber == productList.length) {
      allSelect = true;
    }
    that.setData({
      productList: productList,
      checkedAllStatus: allSelect
    })
    that.countCart();
  },
  // 计算商品的总数
  countCart() {
    let goods = this.data.productList;
    var sumNumber = 0;
    for (let i = 0; i < goods.length; i++) {
      if (goods[i].checked == 1) {
        sumNumber++
      }
    }
    this.setData({
      goodsNumber: sumNumber,
    })
  },
  // 全选与取消全选
  checkedAll: function () {
    let that = this;
    let goodsList = that.data.productList;
    if (that.data.checkedAllStatus) {
      for (let i = 0; i < goodsList.length; i++) {
        goodsList[i].checked = 0
      }
      that.setData({
        checkedAllStatus: false,
        productList: goodsList
      })
    } else {
      for (let i = 0; i < goodsList.length; i++) {
        goodsList[i].checked = 1
      }
      that.setData({
        checkedAllStatus: true,
        productList: goodsList
      })
    }
    that.countCart();
  },
  // 确认选择选中的商品信息
  checkoutOrder: function () {
    let that = this;
    let goodsList = that.data.productList
    let checkedProduct = []
    for (let i = 0; i < goodsList.length; i++) {
      if (goodsList[i].checked == 1) {
        checkedProduct[checkedProduct.length] = goodsList[i].productID
      }
    }
    if (checkedProduct.length == 0) {
      wx.showToast({
        title: '还未选择商品',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.setStorageSync('checkedProductIDS', checkedProduct)
      that.setData({
        checkedProductIDS: checkedProduct,
        selectPopup: false,
      })
      wx.navigateBack()
    }
  },
  // 获取指定团购订单的已选商品
  getTeamOrderInfo(orderId) {
    let that = this;
    util.requestApi(api.getTeamOrderDetails, {
      teamOrderID: orderId
    }).then(function (res) {
      if (res.code == 200) {
        that.setData({
          checkedProductIDS: res.content.productList,
          goodsNumber: res.content.productList.length,
        })
        // 获取可以团购的商品列表
        that.getTeamProductList();
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})